package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
	"strconv"
)

var customers []map[string]string
var customerid int

func main() {
	for {
		fmt.Printf("\033[1;30;35m%s\033[0m\n", `
------------------客户关系管理------------------
		1、添加客户
		2、查看客户
		3、更新客户
		4、删除客户
		5、保存
		6、退出
---------------------------------------------
`)
		var choice int
		fmt.Print("请输入你的选择：")
		fmt.Scan(&choice)

		switch choice {
		case 1:
			//引导用户输入
			fmt.Printf("\033[1;35;45m%s\033[0m\n", "=========添加客户开始=========")
			var name string
			fmt.Print("请输入客户姓名：")
			fmt.Scan(&name)
			var gender string
			fmt.Print("请输入客户性别：")
			fmt.Scan(&gender)
			var age string
			fmt.Print("请输入客户年龄：")
			fmt.Scan(&age)
			var email string
			fmt.Print("请输入客户邮箱：")
			fmt.Scan(&email)

			//添加
			customerid++
			var newCustomer = map[string]string{"cid": strconv.Itoa(customerid), "name": name, "age": age, "gender": gender, "email": email}
			customers = append(customers, newCustomer)
			fmt.Printf("\033[1;35;55m%s\033[0m\n", "=========添加客户结束=========")
		case 2:
			//查看
			fmt.Printf("\033[1;35;45m%s\033[0m\n", "=========客户列表开始=========")
			for _, v := range customers {
				fmt.Printf("编号：%-4s 姓名：%-4s 年龄：%-4s 性别：%-4s 邮箱：%-8s\n", v["cid"], v["name"], v["age"], v["gender"], v["email"])
			}
			fmt.Printf("\033[1;35;55m%s\033[0m\n", "=========客户列表结束=========")
		case 3:
			//修改
			fmt.Printf("\033[1;35;55m%s\033[0m\n", "=========修改客户开始=========")
			for {
				//判断切片是否为空
				if len(customers) == 0 {
					break
				} else {
					var cid string
					fmt.Print("请输入修改的客户编号：")
					fmt.Scan(&cid)
					//判断客户的索引值，-1表示不存在
					var updateIndex = -1
					for index, cust := range customers {
						if cust["cid"] == cid {
							updateIndex = index
						}
					}
					if updateIndex == -1 {
						fmt.Println("该编号不存在！请重新输入")
						continue
					}
					//获取更新的map对象
					updateCustomer := customers[updateIndex]
					//引导用户输入
					var name string
					fmt.Printf("请输入修改客户姓名(%s)：", updateCustomer["name"])
					fmt.Scanln(&name)

					var gender string
					fmt.Printf("请输入修改客户性别(%s)：", updateCustomer["gender"])
					fmt.Scanln(&gender)

					var age string
					fmt.Printf("请输入修改客户年龄(%s)：", updateCustomer["age"])
					fmt.Scanln(&age)

					var email string
					fmt.Printf("请输入修改客户邮箱(%s)：", updateCustomer["email"])
					fmt.Scanln(&email)

					if name != "" {
						updateCustomer["name"] = name
					}
					if gender != "" {
						updateCustomer["gender"] = gender
					}
					if age != "" {
						updateCustomer["age"] = age
					}
					if email != "" {
						updateCustomer["email"] = email
					}
					break
				}
			}
			fmt.Printf("\033[1;35;55m%s\033[0m\n", "=========修改客户结束=========")

		case 4:
			//删除
			fmt.Printf("\033[1;35;55m%s\033[0m\n", "=========删除客户开始=========")
			for {
				//判断切片是否为空
				if len(customers) == 0 {
					break
				} else {
					var cid string
					fmt.Print("请输入删除的客户编号：")
					fmt.Scan(&cid)
					//判断客户的索引值，-1表示不存在
					var deleteIndex = -1
					for index, cust := range customers {
						if cust["cid"] == cid {
							deleteIndex = index
						}
					}
					if deleteIndex == -1 {
						fmt.Println("删除编号不存在！请重新输入")
						continue
					}

					customers = append(customers[:deleteIndex], customers[deleteIndex+1:]...)
					break
				}

			}
			fmt.Printf("\033[1;35;55m%s\033[0m\n", "=========删除客户结束=========")

		case 5:
			//保存
			fmt.Printf("\033[1;35;55m%s\033[0m\n", "=========从内存保存客户信息开始=========")
			data1, _ := json.Marshal(customers)
			ioutil.WriteFile("data.json", data1, 666)
			fmt.Printf("\033[1;35;55m%s\033[0m\n", "=========从内存保存客户信息结束=========")

		case 6:
			//退出，强制退出
			os.Exit(0)
		default:
			fmt.Println("输入不正确。")
		}
	}
}